<?php
namespace Home\Controller;

use Think\Controller;
class QuesController extends BaseController {



    //查询所有问题
    public function getAllQes(){
        $qeslist = M('qes_list');

        //倒叙获取所有问题信息,$all_qes赋值为二维数组，行为数字下标，列为参数下标
        $all_qes = $qeslist->order('qes_id desc')->select();

        //转化时间戳
        foreach($all_qes as $key => $qes){

            $all_qes[$key]['qes_time'] = date('Y-m-d',$qes['qes_time']);

        }

        $return_data = array();
        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($all_qes);
        $return_data['data'] = $all_qes;



        $this->ajaxReturn($return_data);
    }


    //查询我的问题
    public function getMyQes(){
        $qeslist = M('qes_list');

        if(!$_POST['user_id']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：user_id';

            $this->ajaxReturn($return_data);
        }

        //设置条件
        $where = array();
        $where['user_id'] = $_POST['user_id'];

        //查找记录
        $all_qes = $qeslist->where($where)->order('qes_id desc')->select();

        //转换时间戳
        foreach ($all_qes as $key => $value) {
            $all_qes[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
        }

        $return_data = array();
        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($all_qes);
        $return_data['data'] = $all_qes;

        $this->ajaxReturn($return_data);

    }

    //筛选，本学院
    public function screenFaculty(){
      if(!$_POST['user_school']){
         $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:user_school';
            $this->ajaxReturn($return_data);
      }

      $qeslist = M('qes_list');
      $where=array();
      $where['user_school'] = $_POST['user_school'];

      $result = $qeslist->where($where)->order('qes_id desc')->select();

      //转换时间戳
        foreach ($result as $key => $value) {
            $result[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
        }

        $return_data = array();
        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($result);
        $return_data['data'] = $result;

        $this->ajaxReturn($return_data);

    }

    //评论正序倒序
    public function comment_num(){
      if(!$_POST['order']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:order';
            $this->ajaxReturn($return_data);
      }

      $qeslist = M('qes_list');
      if($_POST['mySchool']==1){
          $where=array();
          $where['user_school'] = $_POST['user_school'];
          if($_POST['order'] == '正'){
              $result = $qeslist->where($where)->order('comment_num asc')->select();
          }else if($_POST['order'] == '倒'){
              $result = $qeslist->where($where)->order('comment_num desc')->select();
          }else{
              $return_data = array();
              $return_data['error_code'] = 2;
              $return_data['msg'] = '请输入正确的order';

              $this->ajaxReturn($return_data);
          }
      }
      else{
          if($_POST['order'] == '正'){
              $result = $qeslist->order('comment_num asc')->select();
          }else if($_POST['order'] == '倒'){
              $result = $qeslist->order('comment_num desc')->select();
          }else{
              $return_data = array();
              $return_data['error_code'] = 2;
              $return_data['msg'] = '请输入正确的order';

              $this->ajaxReturn($return_data);
          }
      }


      //转换时间戳
        foreach ($result as $key => $value) {
            $result[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
        }

        $return_data = array();
        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($result);
        $return_data['data'] = $result;

        $this->ajaxReturn($return_data);

    }

    //关注，正序倒序
     public function collect_num(){
      if(!$_POST['order']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:order';
            $this->ajaxReturn($return_data);
      }

      $qeslist = M('qes_list');
      if($_POST['mySchool']==1){
          $where=array();
          $where['user_school'] = $_POST['user_school'];
          if($_POST['order'] == '正'){
              $result = $qeslist->where($where)->order('collect_num')->select();
          }else if($_POST['order'] == '倒'){
              $result = $qeslist->where($where)->order('collect_num desc')->select();
          }else{
              $return_data = array();
              $return_data['error_code'] = 2;
              $return_data['msg'] = '请输入正确的order';

              $this->ajaxReturn($return_data);
          }
      }
      else{
          if($_POST['order'] == '正'){
              $result = $qeslist->order('collect_num')->select();
          }else if($_POST['order'] == '倒'){
              $result = $qeslist->order('collect_num desc')->select();
          }else{
              $return_data = array();
              $return_data['error_code'] = 2;
              $return_data['msg'] = '请输入正确的order';

              $this->ajaxReturn($return_data);
          }
      }


      //转换时间戳
        foreach ($result as $key => $value) {
            $result[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
        }

        $return_data = array();
        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($result);
        $return_data['data'] = $result;

        $this->ajaxReturn($return_data);

    }

    //发布问题
    public function pushNewQes(){
        $key = array('user_id','user_name','user_school','user_faculty','user_picurl','qes_content');
        $return_data = array();

       foreach ($key as $dest => $value) {
            
           if(!$_POST[''.$value]){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:'.$value;
            $this->ajaxReturn($return_data);
           }
       }

       //查找用户是否存在
       $userlist = M('userList');
       $where = array();
       $where['user_id'] = $_POST['user_id'];
       $result = $userlist->where($where)->find();
        if(!$result){
            $return_data['error_code'] = 2;
            $return_data['msg'] = '该用户不存在';

            $this->ajaxReturn($return_data);
       }


       $qeslist = M('qes_list');

       //组装数据
       $data = array();
       $data = $_POST;
       $data['qes_time'] = time();

       //新增记录
       $result = $qeslist->add($data);

       $return_data = array();
       if($result){
        
           $return_data['error_code'] = 0;
           $return_data['msg'] = '数据添加成功';
           $return_data['data'] = $data;
           $return_data['data']['qes_id'] = $result;
       }else{
           $return_data['error_code'] = 2;
           $return_data['msg'] = '数据添加失败';
       }

       $this->ajaxReturn($return_data);
    }

    //根据qes_id查找问题详情
    public function find_one(){
        if(!$_POST['qes_id']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:qes_id';
            $this->ajaxReturn($return_data);
        }

        $where = array();
        $where['qes_id'] = $_POST['qes_id'];

        $qeslist = M('qes_list');
        $result = $qeslist->where($where)->find();

        if($result){
            $result['qes_time'] = date('Y-m-d',$result['qes_time']);
            $return_data['error_code'] = 0;
            $return_data['msg'] = '获取数据成功';
            $return_data['data'] = $result;
        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '该问题不存在';
            $return_data['qes_id'] = $_POST['qes_id'];
        }
        $this->ajaxReturn($return_data);
    }

    //根据qes_id删除问题信息
    public function delete_one(){
        if(!$_POST['qes_id']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:qes_id';
            $this->ajaxReturn($return_data);
        }

        $where = array();
        $where['qes_id'] = $_POST['qes_id'];

        $qeslist = M('qes_list');
        $result = $qeslist->where($where)->delete();

        if($result){
            //删除数据库
            $my_message=M('my_message');
            $my_message->where($where)->delete();
            $answer_qes=M('answer_qes');
            $answer_qes->where($where)->delete();
            $comment_info=M('comment_info');
            $comment_info->where($where)->delete();

            $return_data['error_code'] = 0;
            $return_data['msg'] = '删除数据成功';
        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '删除数据失败（可能是问题不存在）';
        }
        $this->ajaxReturn($return_data);
    }

    //获取我的关注
    public function getMyFollow(){
        $where=array();
        $where['follow_id']=$_POST['user_id'];
        $where['type']=0;

        $answer_qes=M('answer_qes');
        $my=$answer_qes->where($where)->select();

        $qes_list=M('qes_list');
        $display=array();
        foreach ($my as $i => $obj){
            $condition=array();
            $condition['qes_id']=$my[$i]['qes_id'];
            $display[$i]=$qes_list->where($condition)->find();
            $display[$i][qes_time]=date('Y-m-d', $display[$i][qes_time]);
        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($display);
        $return_data['data']=$display;
        $this->ajaxReturn($return_data);
    }

    //搜索全部问题
    public function search_all_qes(){

        if(!$_POST['keyword']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：keyword';
            $this->ajaxReturn($return_data);
        }
        $qeslist = M('qes_list');
        $return_data = array();

        $words = $_POST['keyword'];
        $where['qes_content'] = array('like','%'.$words.'%');
        $list = $qeslist->where($where)->order('qes_id desc')->select();

        //转换时间戳
        foreach ($list as $key => $value) {
            $list[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
        }

        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($list);
        $return_data['data'] = $list;

        $this->ajaxReturn($return_data);
    }

    //搜索我的问题、我的关注
    public function search_my_qes(){

        if(!$_POST['keyword'] || !$_POST['user_id']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：keyword or user_id';
            $this->ajaxReturn($return_data);
        }
        $qeslist = M('qes_list');
        $return_data = array();

        $words = $_POST['keyword'];
        $where['qes_content'] = array('like','%'.$words.'%');

        $display=array();
        if($_POST['myqes']){           //查找我的问题
            //查找记录
            $where['user_id']=$_POST['user_id'];
            $display = $qeslist->where($where)->order('qes_id desc')->select();

            //转换时间戳
            foreach ($display as $key => $value) {
                $display[$key]['qes_time'] = date('Y-m-d',$value['qes_time']);
            }

        }else{
            $condition=array();
            $condition['follow_id']=$_POST['user_id'];
            $condition['type']=0;

            $answer_qes=M('answer_qes');
            $my=$answer_qes->where($condition)->select();


            foreach ($my as $i => $obj){
                $where['qes_id']=$my[$i]['qes_id'];
                $data = $qeslist->where($where)->find();
                if($data){
                    $display[$i]=$data;
                    $display[$i]['qes_time']=date('Y-m-d', $display[$i]['qes_time']);
                }
            }


        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($display);
        $return_data['data']=$display;
        $this->ajaxReturn($return_data);

    }

    //搜索全部文件
    public function search_all_file(){

        if(!$_POST['keyword']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：keyword';
            $this->ajaxReturn($return_data);
        }
        $fileinfo = M('file_info');
        $return_data = array();

        $words = $_POST['keyword'];
        $where['file_name'] = array('like','%'.$words.'%');
        $list = $fileinfo->where($where)->order('file_id desc')->select();

        //转换时间戳
        foreach ($list as $key => $value) {
            $list[$key]['file_time'] = date('Y-m-d',$value['file_time']);
        }


        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($list);
        $return_data['data'] = $list;

        $this->ajaxReturn($return_data);
    }

    //搜索我的资源
    public function search_my_file(){
        if(!$_POST['keyword'] || !$_POST['user_id']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足：keyword or user_id';
            $this->ajaxReturn($return_data);
        }
        $fileinfo = M('file_info');
        $return_data = array();

        $words = $_POST['keyword'];
        $where['file_name'] = array('like','%'.$words.'%');
        $where['user_id'] = $_POST['user_id'];
        $list = $fileinfo->where($where)->order('file_id desc')->select();

        //转换时间戳
        foreach ($list as $key => $value) {
            $list[$key]['file_time'] = date('Y-m-d',$value['file_time']);
        }


        $return_data['error_code'] = 0;
        $return_data['msg'] = '数据获取成功';
        $return_data['count'] = count($list);
        $return_data['data'] = $list;

        $this->ajaxReturn($return_data);
    }

}
